<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增 </a-button>
      </template>
      <template #isDefault="{ record }">
        <Checkbox
          :checked="record['isDefault'] == 1 ? true : false"
          @change="handleIsDefault($event, record)"
        />
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                placement: 'leftBottom',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <ImportComponentDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, h } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useDrawer } from '/@/components/Drawer';
  import ImportComponentDrawer from './ImportComponentDrawer.vue';
  import { columns } from './importComponent.data';
  import { getVisualListImport, setDefaultImportTempalte, delImport } from '/@/api/module/module';
  import { Checkbox } from 'ant-design-vue';
  import { useMessage } from '/@/hooks/web/useMessage';

  export default defineComponent({
    name: 'ImportComponent',
    components: { BasicTable, ImportComponentDrawer, TableAction, Checkbox },
    props: {
      formCode: {
        type: String,
        default: '',
      },
      moduleCode: {
        type: String,
        default: '',
      },
    },
    setup(props) {
      const { createMessage } = useMessage();
      const [registerDrawer, { openDrawer }] = useDrawer();
      const [registerTable, { reload, setLoading }] = useTable({
        title: '',
        api: getVisualListImport,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: [],
          autoSubmitOnEnter: true,
        },
        // searchInfo: {
        //   op: 'search',
        // }, //额外的参数
        pagination: false, //无分页
        beforeFetch: (info) => {
          return {
            ...info,
            moduleCode: props.moduleCode,
          };
        },
        useSearchForm: false,
        showTableSetting: true,
        bordered: true,
        indexColumnProps: { width: 50 },
        showIndexColumn: true,
        actionColumn: {
          width: 80,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
      });

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
          formCode: props.formCode,
          moduleCode: props.moduleCode,
        });
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          formCode: props.formCode,
          moduleCode: props.moduleCode,
          isUpdate: true,
        });
      }

      async function handleDelete(record: Recordable) {
        await delImport({ id: record.id }).then(() => {
          createMessage.success('操作成功');
          handleSuccess();
        });
      }

      function handleSuccess() {
        reload();
      }

      const handleIsDefault = async (e, record) => {
        try {
          const { checked } = e.target;
          const params = {
            id: record.id,
            moduleCode: props.moduleCode,
            isDefault: checked,
          };
          console.log('params', params);
          setLoading(true);
          await setDefaultImportTempalte(params);
          createMessage.success('操作成功');
          handleSuccess();
        } catch (error) {
          console.log(error);
        } finally {
          setLoading(false);
        }
      };

      return {
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        handleIsDefault,
      };
    },
  });
</script>
